<template>
  <!-- 监测点分类 -->
  <div>
    <!-- 搜索框-->

    <div style="background: #fff; height:64px;display:flex">
      <div style="position:relative;bottom:10px;width:100%;">
        <searchComponentsCss :searchData="searchData" :optionsZi="optionsZi" :searchOptionTable="searchOptionTable" :shrinkBoxOpenClose='shrinkBoxOpenClose' @searchCallBack="searchCallBack"></searchComponentsCss>
      </div>
      <div style="position:absolute;right:30px;top:19px;">
        <rightButtons class="right_buttons" @sunS_F='sunS_F' @emptySearch='emptySearch' @configuration='configuration' @goFatherComponent="goFatherComponent" :configurationData="searchData" :btn-switch-configuration="false"></rightButtons>
      </div>
    </div>
    <div class="line"></div>
    <!-- 收缩框 -->
    <!-- <div class="shrinkBox" @click="shrinkBoxOpenClose = !shrinkBoxOpenClose">
      <i :class="shrinkBoxOpenClose?'el-icon-arrow-up':'el-icon-arrow-down'" v-if="numSearch>8"></i>
    </div> -->
    <!-- @clearSearch='clearSearch' -->
    <!-- <div style="border-top:1px solid #E6E6E6;margin:0px 20px;"></div> -->
    <!-- <div class="right_row" style="padding: 25px 30px;">
      </div> -->
    <!-- 表格 -->
    <div class="out" style="margin: 20px 0px;background: #fff;">
      <!--数据列表-->
      <!-- 字典 -->
      <!--新增按钮-->
      <!-- <waterClassfyAdd style="position:relative;right:10px;" v-if="but_add" class="buttonhurdle" @toAdd="toAdd"></waterClassfyAdd> -->
      <el-button style="position:relative;bottom:10px;" type="primary" plain @click="createdAdd" size="mini" icon="el-icon-plus">新建</el-button>
      <dictionariesSchedule :searchData='searchData' :modelType='modelType' @optionData="dictionariesCallBack"></dictionariesSchedule>
      <el-table ref="multipleTable" :data="dataAttribute" v-loading.body="false" border fit highlight-current-row size="mini" :cell-style="{padding:'5px 0'}" @selection-change="handleSelectionChange" @sort-change="sortChangeTable" max-height="511px">
        <el-table-column align="center" type="selection" :show-overflow-tooltip="true" width="50px"></el-table-column>
        <el-table-column align="center" label="序号" type="index" :index="indexMethod" width="100px"></el-table-column>
        <el-table-column align="center" label="分类名称" prop="name"></el-table-column>
        <el-table-column align="center" label="备注" prop="remarks" width="250px"></el-table-column>
        <el-table-column align="center" label="点位标签" width="250px">
          <template slot-scope="scope">
            <img :src="scope.row.icon" style="height:25px">
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" v-if="but_look || but_edit ||but_del" width="250px">
          <template slot-scope="scope" class="do">
            <!-- <el-button v-if="but_look" style="color:#0B9AC3;border:none;" icon="el-icon-view" circle @click="makeExaId(scope.row.id)"></el-button> -->
            <!-- <modificationi v-if="but_edit" class="inline_block" :attributeOrder="searchData" :optionsZi="optionsZi" :searchOptionTable="searchOptionTable" :modData="scope.row" @modifyData="modifyData"></modificationi> -->
            <el-tooltip content="修改">
              <el-button style="color:#0B9AC3;border:none;" icon="el-icon-edit" circle @click="editId(scope.row)"></el-button>
            </el-tooltip>
            <!-- 删除按钮 -->
            <el-button v-if="but_del" style="color:#C90010;border:none;" icon="el-icon-delete" circle @click="deleteList(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background class="pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pagesize" layout="total, prev, pager, next, sizes, jumper" :total="pagetotal"></el-pagination>
    </div>
    <!-- 新增模态框 -->
    <el-dialog title="新增分类" :visible.sync="addFormVisible">
      <el-form :model="addform" :rules="rules">
        <el-form-item label="分类名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="addform.name" autocomplete="off" size="small" style="width:250px;"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="addform.remarks" autocomplete="off" size="small" style="width:250px;"></el-input>
        </el-form-item>
      </el-form>
      <div>
        <span style="font-weight:700;font-size:14px;">设置点位标签</span>
        <ul style="list-style:none;display:flex;width:500px;flex-flow: wrap;">
          <li v-for="(item,index) in liList" v-bind:key="index" v-on:click="addIf(index)" style="margin-left:20px;margin-top:10px;height:55px;width:55px;border:1px solid #F2F2F2;text-align:center;padding-top:5px;position: relative;">
            <div>
              <img :src="item.name" />
              <img :src="ic" v-if="index==current" style="position:absolute;bottom:40px;right:0;" />
            </div>
          </li>
          <!-- <li style="margin-left:20px;">
            <div>
              <img :src="aSvg" />
            </div>
          </li>
          <li style="margin-left:20px;">
            <div>
              <img :src="aSvg" />
            </div>
          </li> -->
        </ul>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addFormVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="toAdd" size="mini">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改模态框 -->
    <el-dialog title="修改分类" :visible.sync="editFormVisible">
      <el-form :model="editform" :rules="rules">
        <el-form-item label="分类名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="editform.name" autocomplete="off" size="small" style="width:250px;"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="editform.remarks" autocomplete="off" size="small" style="width:250px;"></el-input>
        </el-form-item>
      </el-form>
      <div>
        <span style="font-weight:700;font-size:14px;">设置点位标签</span>
        <ul style="list-style:none;display:flex;width:500px;flex-flow: wrap;">
          <li v-for="(item,index) in liList" v-bind:key="index" v-on:click="addIf(index)" style="margin-left:20px;margin-top:10px;height:55px;width:55px;border:1px solid #F2F2F2;text-align:center;padding-top:5px;position: relative;">
            <div>
              <img :src="item.name" />
              <img :src="ic" v-if="index==current" style="position:absolute;bottom:40px;right:0;" />
            </div>
          </li>
        </ul>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="toEdit" size="mini">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 查看组件 -->
    <examine :DialogVisibleMod="DialogVisibleExamine" :modData="examineData" :optionsZi="optionsZi" :searchOptionTable="searchOptionTable" :attributeOrder="searchData" @examineDialog="examineDialog"></examine>
  </div>
</template>
<script>
import waterClassfyAdd from "@/views/components/Schedule/waterClassfyAdd.vue";
import dictionariesSchedule from "@/views/components/FLPage/dictionariesSchedule.vue";
import ic from "@/assets/image/shcedule/ic.svg";
import aSvg from "@/assets/image/shcedule/在线监测1.svg";
import bSvg from "@/assets/image/shcedule/在线监测2.svg";
import cSvg from "@/assets/image/shcedule/在线监测3.svg";
import dSvg from "@/assets/image/shcedule/在线监测4.svg";
import eSvg from "@/assets/image/shcedule/在线监测5.svg";
import fSvg from "@/assets/image/shcedule/在线监测6.svg";
import gSvg from "@/assets/image/shcedule/在线监测7.svg";
import hSvg from "@/assets/image/shcedule/在线监测8.svg";
import iSvg from "@/assets/image/shcedule/在线监测9.svg";
import jSvg from "@/assets/image/shcedule/在线监测10.svg";
import kSvg from "@/assets/image/shcedule/在线监测11.svg";
import lSvg from "@/assets/image/shcedule/在线监测12.svg";
import mSvg from "@/assets/image/shcedule/在线监测13.svg";
import nSvg from "@/assets/image/shcedule/在线监测14.svg";
//引入组件
import {
  SearchCForL,
  searchComponents,
  searchComponentsCss,
  examine,
  rightButtons,
  dictionaries,
  dictionariesPlus
  // importXlsx
} from "@/views/components/FLPage";
//引入组件
import {
  buttonhurdle,
  importXlsx,
  batches,
  derivedTbale,
  importTbale,
  modificationi,
  downloadTemplate
} from "@/views/components/revenueFLpage";
import { SAVE_CHECK_BOXS, GAIN_CHECK_BOXS } from "@/views/components/FLPage/fun/checkBoxFun";
import {
  addFlClassify, //增加
  deleteFlClassify, //删除
  searchFlClassify, //分页查询
  modifyFlClassify, //修改
  getFlClassify, //根据ID获取
  classexprotXls,//导出xlsx数据
  classdownTem //导出下载模板
} from "@/api/schedule/base/flpipeMonitorPoint/index";
export default {
  components: {
    waterClassfyAdd,
    buttonhurdle,
    modificationi,
    examine,
    searchComponents,
    searchComponentsCss,
    importXlsx,
    rightButtons,
    dictionaries,
    dictionariesPlus,
    dictionariesSchedule,
    batches,
    derivedTbale,
    importTbale,
    downloadTemplate
  },
  name: 'FlClassify',
  data () {
    return {
      //标签图片数据
      current: 0,
      liList: [
        {
          name: aSvg,
          id: "1"
        },
        {
          name: bSvg,
          id: "2"
        },
        {
          name: cSvg,
          id: "3"
        },
        {
          name: dSvg,
          id: "4"
        },
        {
          name: eSvg,
          id: "5"
        },
        {
          name: fSvg,
          id: "6"
        },
        {
          name: gSvg,
          id: "7"
        },
        {
          name: hSvg,
          id: "8"
        },
        {
          name: iSvg,
          id: "9"
        },
        {
          name: jSvg,
          id: "10"
        },
        {
          name: kSvg,
          id: "11"
        },
        {
          name: lSvg,
          id: "12"
        },
        {
          name: mSvg,
          id: "13"
        },
        {
          name: nSvg,
          id: "14"
        },
      ],
      ic,
      //表单验证
      rules: {
        name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
      },
      //新增
      addFormVisible: false,
      addform: {
        name: "",
        remarks: "",
      },
      //修改
      editFormVisible: false,
      editform: "",
      editid: "",
      //
      activeName: 'fourth',
      but_add: true,
      but_delBatches: true,
      but_look: true,
      but_edit: true,
      but_del: true,
      but_derivedTbale: true,
      but_importTbale: true,
      but_downloadTemplate: true,
      // lookOver:true,
      // editField:true,
      // Deletebu:true,
      disabled: false,
      numSearch: 0,
      //   收缩框开关
      shrinkBoxOpenClose: false,
      importUrl: 'api/schedule/flClassify/importXls',
      //搜索框
      suoS: {},
      //   搜索组件的数据 columnSize
      searchData: [
        {
          EN: "name",
          CN: "分类名称",
          value: "",
          type: "input",
          err: "长度为1-3位的数字!",
          Exp: "^[0-9]+(\\.[0-9]{1,3})?$",
          // typeGroupCode: 'monitor_point_type',
          sign: true,
          searchSign: true,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
          configurationSign: true
        },
      ],
      DialogVisibleExamine: false,

      //查看的数据
      examineData: {},
      multipleSelection: [],

      //分页
      currentPage: 1, //当前页
      pagesize: 10, //每页条数
      pagetotal: 0, //总条数,
      modelType: 'table',
      optionsZi: [],
      searchOptionTable: [],
      dataAttribute: []
    }
  },
  computed: {
    optionDatas () {
      return function (arr, val) {
        if (arr) {
          for (let i = 0; i < arr.length; i++) {
            if (arr[i].value == val) {
              return arr[i].label;
            }
          }
        }
      }
    },
    customMethod () {
      return function (item) {
        return 'custom';
      }
    }
  },
  created () {
    this.forData(this.currentPage, this.pagesize, this.suoS);
  },
  async created () {
    this.getSSearchNum()
    let userId = this.$store.state.user.id;
    let routerName = this.$route.path;
    let peizhiData = await GAIN_CHECK_BOXS(userId, routerName);
    //2.判断数据库里面是否有 以前存过的配置信息
    if (peizhiData != '') {
      //3.解析(序列化)
      this.searchData = JSON.parse(peizhiData);
    }
  },
  methods: {
    addIf (index) {
      this.current = index
    },
    createdAdd () {
      this.addFormVisible = true
    },
    // 搜索框
    handleClick (tab, event) {
      console.log(tab, event);
    },
    getSSearchNum () {
      let searchData = this.searchData
      let num = 0
      for (let i = 0; i < searchData.length; i++) {
        const element = searchData[i];
        if (element.searchSign) {
          num += 1
        }
        else {
          num = num
        }
      }
      this.numSearch = num
    },
    // 排序
    sortChangeTable (obj) {
      // 上升
      let prop = obj.prop
      // 排序  需求  将驼峰形式的字段 改为 下划线形式的字段
      let abc = prop.replace(/([A-Z])/g, "_$1").toLowerCase()
      if (obj.order === 'ascending') {
        let suoS = this.suoS
        suoS.sortName = abc
        suoS.sortType = obj.order
        this.forData(this.currentPage, this.pagesize, suoS); //刷新列表

      } else if (obj.order === 'descending') {
        let suoS = this.suoS
        suoS.sortName = abc
        suoS.sortType = obj.order
        this.forData(this.currentPage, this.pagesize, suoS); //刷新列表
        // 下降
      } else {
        // 正常
        let suoS = this.suoS
        suoS.sortName = abc
        suoS.sortType = ''
        this.forData(this.currentPage, this.pagesize, suoS); //刷新列表
      }
    },
    handleSuccessImport (newValue) {
      if (newValue == '导入成功') {
        this.forData(this.currentPage, this.pagesize, this.suoS);
      }
    },
    //配置的子组件通知
    async goFatherComponent (OverData) {
      //1.修改父组件的视图***************
      this.searchData = JSON.parse(JSON.stringify(OverData));
      //2.保存’配置‘的更改
      let obj = {};
      //三个参数都是string类型***********
      obj["userId"] = this.$store.state.user.id; //1
      obj["routerName"] = this.$route.path; ///jiChuZiLiao/flBook
      obj["stringData"] = JSON.stringify(OverData); //相当于this.searchData转成了字符串格式    [{"EN":"id","CN":"主键","value":""...
      //封装好的发请求方法*****************
      let save_check_boxs = await SAVE_CHECK_BOXS(obj);
    },
    // 配置
    configuration (msg) {
      if (msg == '配置') {
        console.log(msg);
      }
    },
    // 重置
    emptySearch (msg) {
      if (msg == '重置') {
        this.suoS = {};
        let suoS = this.suoS
        suoS.communityCode = this.areaCode
        this.forData(this.currentPage, this.pagesize, suoS);
      }
    },
    // 字典组件回调
    dictionariesCallBack ([a = {}, b = {}]) {
      // 正常字典
      this.optionsZi = a;
      // 跨表字典
      this.searchOptionTable = b;
      this.forData(this.currentPage, this.pagesize, this.suoS);
    },
    //搜索
    sunS_F (msg) {
      if (msg == '搜索') {
        this.currentPage = 1;
        this.forData(this.currentPage, this.pagesize, this.suoS); //刷新列表
      }
    },
    //查询组件回调
    searchCallBack (newData) {
      let obj = {};
      for (let i = 0; i < newData.length; i++) {
        obj[newData[i].EN] = newData[i].value;
      }
      this.suoS = obj;
    },
    //渲染数据初始
    forData (page, size, searchData) {
      searchFlClassify(page, size, searchData).then(response => {
        this.pagetotal = response.data.total;
        this.dataAttribute = response.data.list;
        if (this.dataAttribute.length == 0) {
          this.disabled = true
        } else {
          this.disabled = false
        }
        //转换图片
        this.dataAttribute = this.dataAttribute.map((item) => {
          let index = parseInt(item.icon)
          item.icon = this.liList[index].name
          item['iconIndex'] = index
          return item
        })
      }).catch(
        this.disabled = true
      );
    },
    //添加用户--新增表单组件传来的参数
    toAdd () {
      let obj = {
        name: this.addform.name,
        remarks: this.addform.remarks,
        icon: this.current
      };

      addFlClassify(obj).then(response => {
        this.forData(this.currentPage, this.pagesize, this.suoS); //刷新列表
        if (response.flag) {
          this.$message({
            message: response.message,
            type: "success"
          });
        }
        else {
          this.$message({
            message: response.message,
            type: "warning"
          });

        }
      }).catch(() => {
        this.$message.error("添加失败");
      })
      this.addFormVisible = false
    },
    //按钮栏传递了删除命令
    toDelete (orderstring) {
      this.$confirm("确认删除？").then(_ => {
        let idArr = [];
        for (let i = 0; i < this.deleteDataList.length; i++) {
          idArr.push(this.deleteDataList[i].id);
        }
        if (idArr.length == 0) {
          this.$message({
            message: '请选择要删除的数据',
            type: 'warning'
          })
          return;
        }
        deleteFlClassify(idArr).then(response => {
          this.forData(this.currentPage, this.pagesize, this.suoS); //刷新列表
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        }).catch(error => {
          this.$message({
            message: '删除失败',
            type: 'warning'
          })
        });
      }).catch(_ => { });
    },
    //单项删除
    deleteList (id) {
      let arrId = [id];
      this.$confirm("确认删除？").then(_ => {
        deleteFlClassify(arrId).then(response => {
          this.forData(this.currentPage, this.pagesize, this.suoS); //刷新列表
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        });
        done();
      }).catch(_ => { });
    },
    //查看
    makeExaId (id) {
      getFlClassify(id).then(response => {
        let copyDialog = true;
        this.DialogVisibleExamine = copyDialog;
        // console.log(response);
        this.examineData = response.data;
      }).catch(error => {
        alert("获取失败");
      });
    },
    //查看关闭弹窗
    examineDialog (dialogVisibleMod) {
      this.DialogVisibleExamine = false;
    },

    //修改
    modifyData (value, modData) {
      if (value) {
        let id = modData.id;
        modifyFlClassify(id, modData).then(response => {
          this.forData(this.currentPage, this.pagesize, this.suoS); //刷新列表
          if (response.flag) {
            this.$message({
              message: response.message,
              type: "success"
            });
          }
          else {
            this.$message({
              message: response.message,
              type: "warning"
            });

          }
        }).catch(error => {
          this.$message({
            message: '修改失败',
            type: 'warning'
          })
          this.forData(this.currentPage, this.pagesize, this.suoS); //刷新列表
        });
      } else {
        this.forData(this.currentPage, this.pagesize, this.suoS); //刷新列表
      }
    },
    //
    editId (value) {
      this.editform = value
      this.editid = value.id
      this.editFormVisible = true
      this.current = parseInt(value.iconIndex)
    },
    toEdit () {
      let obj = {
        name: this.editform.name,
        remarks: this.editform.remarks,
        icon: this.current
      };
      modifyFlClassify(this.editid, obj).then(response => {
        this.forData(this.currentPage, this.pagesize, this.suoS); //刷新列表
        if (response.flag) {
          this.$message({
            message: response.message,
            type: "success"
          });
        }
        else {
          this.$message({
            message: response.message,
            type: "warning"
          });

        }
      }).catch(error => {
        this.$message({
          message: '修改失败',
          type: 'warning'
        })
        this.forData(this.currentPage, this.pagesize, this.suoS); //刷新列表
      });
      this.editFormVisible = false
    },
    //表格选中事件
    handleSelectionChange (val) {
      this.multipleSelection = val;
      //传递删除的参数
      this.deleteDataList = val;
    },

    //分页
    handleSizeChange (val) {
      console.log("每页 " + val + " 条");
      this.pagesize = val;
      this.forData(this.currentPage, val, this.suoS);
    },
    handleCurrentChange (val) {
      console.log("当前页: " + val);
      this.currentPage = val;
      this.forData(val, this.pagesize, this.suoS);
    },
    // 序号
    indexMethod (index) {
      var currentPage = this.currentPage;
      var pagesize = this.pagesize;
      this.$nextTick(function () {
        currentPage = this.currentPage;
        pagesize = this.pagesize;
      });
      return currentPage * pagesize - pagesize + 1 + index
    },
    // 下载模板请求
    exprotXlsx: classexprotXls,
    downloadFile: classdownTem
    //工具栏组件传来的参数
    // FTableFunction(attributeOrderCloneTwiceL) {
    //   this.attributeOrder = attributeOrderCloneTwiceL;
    // },
  }
};
</script>
<style scoped src="@/styles/allCssP.css"></style>
<style lang="scss" scoped>
.FlClassify {
  // padding: 20px 30px 0 30px;
  background: #f2f3f5;
  .shrinkBox {
    text-align: center;
    font-weight: 700;
    font-size: 20px;
    height: 25px;
    // border-bottom:1px solid #E6E6E6;
  }
  /deep/.el-table--mini {
    .gutter {
      display: table-cell !important;
    }
    .el-checkbox__inner {
      width: 12px;
      height: 12px;
    }
    /deep/.el-table__body-wrapper {
      .el-table__body {
        tbody {
          .el-table__row {
            height: 45px !important;
          }
        }
      }
    }
    // 改变表头的高度  改为45px
    .el-table__header-wrapper {
      .el-table__header {
        .has-gutter {
          tr {
            th {
              padding: 5px 0 !important;
              height: 34px;
              line-height: 34px;
            }
          }
        }
      }
    }
    // 表头操作固定定位
    .el-table__fixed-right {
      .el-table__header {
        thead {
          tr {
            th {
              padding: 5px 0 !important;
              height: 34px;
              line-height: 34px;
            }
          }
        }
      }
    }
  }
  .right_row {
    display: flex !important;
    justify-content: start;
    align-items: center;
    .buttonhurdle {
      // padding: 0px;
      // padding-right: 15px;
      // position: relative;
      // right: 10px;
    }
    .right_buttons {
      position: absolute;
      right: 30px;
      padding: 0px;
    }
  }
  .do {
    display: flex;
  }
  .pagination {
    text-align: left;
    margin-top: 15px;
  }
}
</style>
